package com.tuanbiao.ui.setting;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.abiao.lib_utils.ActivityUtils;
import com.amap.api.maps.offlinemap.OfflineMapActivity;
import com.tuanbiao.AppConfig;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.constants.SettingType;
import com.tuanbiao.constants.UserConstants;
import com.tuanbiao.controllers.SettingController;
import com.tuanbiao.databinding.ActivitySettingBinding;
import com.tuanbiao.dialog.BaseDialog;
import com.tuanbiao.dialog.InputDialog;
import com.tuanbiao.dialog.MessageDialog;
import com.tuanbiao.events.SettingDeviceEvent;
import com.tuanbiao.keyevent.KeyEventHelper;
import com.tuanbiao.keyevent.KeyEventUtils;
import com.tuanbiao.keyevent.listener.OnSettingKeyDownListener;
import com.tuanbiao.models.SettingModel;
import com.tuanbiao.presenter.setting.SettingPresenter;
import com.tuanbiao.ui.account.LoginActivity;
import com.tuanbiao.ui.ManualActivity;
import com.tuanbiao.ui.log.LogOperateActivity;
import com.tuanbiao.utils.ClickUtil;

import org.greenrobot.eventbus.EventBus;

import timber.log.Timber;

public class SettingActivity extends BasePresenterActivity<SettingPresenter, ActivitySettingBinding> implements SettingController.View {
    private StringBuffer keyDownBuf = new StringBuffer();

    public static void start(Context context) {
        Intent starter = new Intent(context, SettingActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected ActivitySettingBinding getBindingView() {
        return ActivitySettingBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        binding.sbAbout.setOnClickListener(view -> {
            AboutActivity.start(getActivity());
        });

        binding.sbLoginTime.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            mPresenter.toggleLoginTime();
        });
        binding.ivToolbarBack.setOnClickListener(getBackListener());
        binding.sbDeviceInfo.setOnClickListener(v -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            DeviceInfoActivity.start(getActivity());
        });
        binding.sbChangeServer.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showChangePasswordDialog(SettingType.CHANGE_SERVICE);
        });
        binding.sbManual.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            ManualActivity.start(getActivity());
        });
        binding.sbMore.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            SettingMoreActivity.start(getActivity());
        });
        binding.sbControlPanelInfo.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            SettingControlPanelActivity.start(getActivity());
        });
        binding.sbSetLocation.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            SettingLocationActivity.start(getActivity());
        });
        binding.sbChangeDeviceNum.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showChangePasswordDialog(SettingType.CHANGE_DEVICE_NUM);
        });
        binding.sbLogout.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showLogoutDialog();
        });
        binding.sbOfflineMap.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            startActivity(OfflineMapActivity.class);
        });
    }

    private void showLogoutDialog() {
        new MessageDialog.Builder(getActivity())
                .setTitle("提示")
                .setMessage("确定退出登录吗？")
                .setListener(dialog -> {
                    mPresenter.logout();
                }).show();

    }

    private void showChangePasswordDialog(SettingType type) {
        if (AppConfig.isDebug()) {
            switch (type) {
                case CHANGE_DEVICE_NUM:
                    startChangeDeviceNum();
                    break;
                case CHANGE_SERVICE:
                    startChangeService();
                    break;
                default:
                    break;
            }
        } else {
            String password = mPresenter.getPassword();
            new InputDialog.Builder(getActivity()).setTitle("提示")
                    .setHint("请输入密码")
                    .setAutoDismiss(false)
                    .setListener(new InputDialog.OnListener() {
                        @Override
                        public void onConfirm(BaseDialog dialog, String content) {
                            if (TextUtils.isEmpty(content)) {
                                showMessage("请输入密码");
                                return;
                            }
                            mPresenter.checkPassword(content, password, type);
                            dialog.dismiss();
                        }

                        @Override
                        public void onCancel(BaseDialog dialog) {
                            dialog.dismiss();
                        }
                    }).show();
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Timber.i("onKeyDown:" + keyCode);
        KeyEventHelper.getInstance().checkSettingKeyDown(keyCode, new OnSettingKeyDownListener() {

            @Override
            public void onMoreVisibility(boolean visible) {
                binding.sbMore.setVisibility(visible ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onToggleModel() {
                mPresenter.toggleModel();
            }

            @Override
            public void onLogOperate() {
                LogOperateActivity.start(getActivity());
            }

            @Override
            public void onSetting() {
                showSettingPasswordDialog();
            }
        });
        return super.onKeyDown(keyCode, event);
    }

    private void showSettingPasswordDialog() {
        if (AppConfig.showPassword()) {
            startDeviceSetting(UserConstants.Type.DEVELOP);
        } else {
            new InputDialog.Builder(getActivity())
                    .setTitle("提示")
                    .setHint("请输入密码")
                    .setAutoDismiss(false)
                    .setListener(new InputDialog.OnListener() {
                        @Override
                        public void onConfirm(BaseDialog dialog, String content) {
                            if (TextUtils.isEmpty(content)) {
                                showMessage("请输入密码");
                                return;
                            }
                            checkPassword(content);
                            dialog.dismiss();
                        }

                        @Override
                        public void onCancel(BaseDialog dialog) {
                            dialog.dismiss();
                        }
                    }).show();
        }
    }

    private void checkPassword(String content) {
        if (TextUtils.equals(content, KeyEventUtils.getInstance().getLoginF2f3Password2())) {
            startDeviceSetting(UserConstants.Type.TEST);
        } else {
            showMessage("密码错误");
        }
    }

    private void startDeviceSetting(int type) {
        EventBus.getDefault().postSticky(new SettingDeviceEvent(type));
        SettingDeviceActivity.start(getActivity());
    }

    private void checkKeyEvent() {
        if (TextUtils.equals("F2F3", keyDownBuf) || TextUtils.equals("F3F2", keyDownBuf)) {
            if (ClickUtil.isFastClick()) {
                LogOperateActivity.start(getActivity());
            }
        } else {
            postDelayed(() -> keyDownBuf = new StringBuffer(), 1000);
        }
    }

    @Override
    protected SettingPresenter createPresenter() {
        return new SettingPresenter(new SettingModel(), this);
    }

    @Override
    public void showWorkModel(String workModeName) {
        binding.sbWorkModel.setRightText(workModeName);
    }

    @Override
    public void showControlPanelChange(boolean show) {
        binding.sbControlPanelInfo.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    public void showWorkModelChange(boolean show) {
        binding.sbWorkModel.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    public void showServerChange(boolean show) {
        binding.sbChangeServer.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    public void showLocalChange(boolean show) {
        binding.sbSetLocation.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    public void showDeviceNumChange(boolean show) {
        binding.sbChangeDeviceNum.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    public void showLoginTime(String time) {
        binding.sbLoginTime.setRightText(time);
    }

    @Override
    public void startLogin() {
        ActivityUtils.finishAllActivities();
        LoginActivity.start(getActivity());
        finish();
    }

    @Override
    public void startChangeDeviceNum() {
        SettingDeviceNumActivity.start(getActivity());
    }

    @Override
    public void startChangeService() {
        SettingServiceActivity.start(getActivity());
    }

}